﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Portal.Filter;
using Portal.Models;
using SoEasyPlatform.LayUiExample.Enties.DbModels;
using SqlSugar;
using WMS.DtoModels;

namespace Portal.Controllers
{
    /// <summary>
    /// 菜单权限
    /// </summary>
    [LoginCheckFilter(IsCheck = true)]
    public class MenuPermissionController : Controller
    {
        private readonly SqlSugarClient _sqlSugarClient;
        

        public MenuPermissionController(SqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        public IActionResult Index()
        {
            var loginUserStr = this.HttpContext.Session.GetString(GlobalVariable.LoginUser);
            var loginUser = JsonConvert.DeserializeObject<sys_user>(loginUserStr);

            if (loginUser.type == GlobalVariable.UserTypeSuper) //超级管理员
            {
                var menuList = _sqlSugarClient.Queryable<sys_permission>()
                    .Where(x => x.available == GlobalVariable.AvailableTrue && x.type == GlobalVariable.TypeMenu)
                    .ToList();

                var resultMenu = GetMenuLogic(menuList);

                return Json(resultMenu);
            }
            else   //普通用户需要根据 用户Id+角色+权限去查询
            {
                var rids = this._sqlSugarClient.Queryable<sys_role_user>().Where(x => x.uid == loginUser.id).Select(x=>x.rid).ToList();

                var pid = this._sqlSugarClient.Queryable<sys_role_permission>().Where(x => rids.Contains(x.rid)).Select(x=>x.pid).ToList();
               
                var menuList = this._sqlSugarClient.Queryable<sys_permission>().Where(x =>
                    pid.Contains(x.id) && x.available == GlobalVariable.AvailableTrue &&
                    x.type == GlobalVariable.TypeMenu).ToList();

                var resultMenu = GetMenuLogic(menuList);

                return Json(resultMenu);
            }


           
        }

        private List<MenuPermissionDto> GetMenuLogic(List<sys_permission> menuList)
        {
            var resultMenu = new List<MenuPermissionDto>();
            //拿到一级菜单
            var oneMenus = menuList.Where(x => x.pid == 1).ToList();
            foreach (var oneMen in oneMenus)
            {
                var curOneMenu = new MenuPermissionDto
                {
                    Title = oneMen.title,
                    Icon = oneMen.icon,
                    Href = oneMen.href,
                    Spread = oneMen.open == GlobalVariable.OpenTrue
                };

                //拿到当前一级菜单下的子菜单
                var twoMenus = menuList.Where(x => x.pid == oneMen.id);

                foreach (var twoMenu in twoMenus)
                {
                    var curTwoMenu = new MenuPermissionDto
                    {
                        Title = twoMenu.title,
                        Icon = twoMenu.icon,
                        Href = twoMenu.href,
                        Spread = twoMenu.open == GlobalVariable.OpenTrue
                    };

                    curOneMenu.Children.Add(curTwoMenu);
                }

                resultMenu.Add(curOneMenu);
            }

            return resultMenu;
        }
    }
}